package com.cosmoplat.xikai.business.controller.scada;

import com.cosmoplat.xikai.business.domain.FaultConfiguration;
import com.cosmoplat.xikai.business.domain.dto.scada.FaultConfigurationDto;
import com.cosmoplat.xikai.business.domain.dto.scada.FaultInformationDto;
import com.cosmoplat.xikai.business.enums.DeviceWarnEnum;
import com.cosmoplat.xikai.business.service.FaultInformationService;
import com.cosmoplat.xikai.common.core.controller.BaseController;
import com.cosmoplat.xikai.common.core.domain.AjaxResult;
import com.cosmoplat.xikai.common.core.page.TableDataInfo;
import com.cosmoplat.xikai.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

@RestController
@RequestMapping("/fault/information")
@Api(tags = "故障信息配置", value = "故障信息配置")
public class FaultInformationController extends BaseController {

    @Autowired
    private FaultInformationService faultInformationService;


    /**
     * 下载文件
     *
     * @param response
     * @throws Exception
     */
    @ApiOperation(value = "下载模板")
    @GetMapping("/download")
    @PreAuthorize("@ss.hasPermi('scada:faultInformation:download')")
    public void download(HttpServletResponse response) throws Exception {
        ArrayList<FaultConfiguration> list = new ArrayList<>();
        new ExcelUtil<>(FaultConfiguration.class).exportExcel(response, list, "故障信息配置导入模板", "故障信息配置模板");

    }

    /**
     * 批量保存
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/batchSave")
    @ApiOperation(value = "批量导入")
    @PreAuthorize("@ss.hasPermi('scada:faultInformation:batchSave')")
    public AjaxResult batchSave(MultipartFile multipartFile) throws Exception {
        return faultInformationService.batchSave(multipartFile);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @DeleteMapping("/del/{ids}")
    @ApiOperation(value = "删除")
    @PreAuthorize("@ss.hasPermi('scada:faultInformation:del')")
    public AjaxResult remove(@PathVariable Long[] ids) throws Exception {
        return faultInformationService.removeByIds(ids);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/query")
    @ApiOperation(value = "查询id详情")
    @PreAuthorize("@ss.hasPermi('scada:faultInformation:query')")
    public AjaxResult queryById(@RequestParam("id") int id) throws Exception {
        return faultInformationService.getById(id);
    }

    /**
     * 保存
     *
     * @param faultInformationDto
     * @return
     * @throws Exception
     */
    @PutMapping("/save")
    @ApiOperation(value = "数据保存修改")
    @PreAuthorize("@ss.hasPermi('scada:faultInformation:save')")
    public AjaxResult save(@RequestBody FaultInformationDto faultInformationDto) throws Exception {
        return faultInformationService.save(faultInformationDto);
    }

    /**
     * 设置请求分页数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询")
    @PreAuthorize("@ss.hasPermi('scada:faultInformation:list')")
    public TableDataInfo list(FaultConfigurationDto dto) {
        startPage();
        return getDataTable(faultInformationService.selectConfigList(dto));
    }

    /**
     * 获取故障名称列表
     */
    @GetMapping("/faultName/list")
    @ApiOperation(value = "获取故障名称列表")
    @PreAuthorize("@ss.hasPermi('scada:faultInformation:faultNameList')")
    public AjaxResult faultNameList() {
        ArrayList<String> identiflerList = DeviceWarnEnum.getIdentiflerList();
        return AjaxResult.success(identiflerList);
    }
}
